<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '审批'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <template>
        <el-radio v-model="radio" label="1">同意</el-radio>
        <el-radio v-model="radio" label="2">拒绝</el-radio>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        radio: '1',
        visible: false,
        dataForm: {
          id: 0,
          remarks: '',
          isdeleted: '',
          customerid: '',
          retreattime: '',
          retreattype: '',
          retreatreason: '',
          auditstatus: '',
          auditperson: '',
          audittime: ''
        },
        dataRule: {
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          isdeleted: [
            { required: true, message: '逻辑删除标记（0：显示；1：隐藏）不能为空', trigger: 'blur' }
          ],
          customerid: [
            { required: true, message: '客户ID不能为空', trigger: 'blur' }
          ],
          retreattime: [
            { required: true, message: '退住时间不能为空', trigger: 'blur' }
          ],
          retreattype: [
            { required: true, message: '退住类型 0-正常退住  1-死亡退住 2-保留床位不能为空', trigger: 'blur' }
          ],
          retreatreason: [
            { required: true, message: '退住原因不能为空', trigger: 'blur' }
          ],
          auditstatus: [
            { required: true, message: '审批状态  0-已提交 1-同意  2-拒绝不能为空', trigger: 'blur' }
          ],
          auditperson: [
            { required: true, message: '审批人不能为空', trigger: 'blur' }
          ],
          audittime: [
            { required: true, message: '审批时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/backdown/backdown/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.remarks = data.backDown.remarks
                this.dataForm.isdeleted = data.backDown.isdeleted
                this.dataForm.customerid = data.backDown.customerid
                this.dataForm.retreattime = data.backDown.retreattime
                this.dataForm.retreattype = data.backDown.retreattype
                this.dataForm.retreatreason = data.backDown.retreatreason
                this.dataForm.auditstatus = data.backDown.auditstatus
                this.dataForm.auditperson = data.backDown.auditperson
                this.dataForm.audittime = data.backDown.audittime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backdown/backdown/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'remarks': this.dataForm.remarks,
                'isdeleted': this.dataForm.isdeleted,
                'customerid': this.dataForm.customerid,
                'retreattime': this.dataForm.retreattime,
                'retreattype': this.dataForm.retreattype,
                'retreatreason': this.dataForm.retreatreason,
                'auditstatus': this.dataForm.auditstatus,
                'auditperson': this.dataForm.auditperson,
                'audittime': this.dataForm.audittime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
